define(['moment', 'module/moduleMobileSMT/program/SMT02/MaterialStation.service'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config', '$MaterialStation',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config, $MaterialStation) {

            // 流程
            var levels = [
                [
                    'process_mfu',
                    'process_machine',
                    'process_plot_no',
                    'process_feederlist_no'
                ],
                [
                    'process_feeder',
                    'process_material',
                    'process_slot_no'
                ]
            ];

            let element_barcode_no;//pda掃描
            $scope.init = function(){
                $scope.user_name = config.cache.name;
                //取得是否啟用禁用管理系統參數
                $scope.FBD = config.cache.sys_parameter['SMTInfo.FBD'];
                $scope.envInfo = $filter('translate')('Login.env.'+config.setting.environment);
                // 預設流程狀態
                $scope.level_index = 0;
                $scope.phases_index = 0;
                $scope.phases = levels[$scope.level_index];
                //正背板
                $scope.panelList = [{"labelname":$filter('translate')('SMT02.panel.options_1'),"labelval":"1"},{"labelname":$filter('translate')('SMT02.panel.options_2'),"labelval":"2"}];
                $scope.panel_val = $scope.panelList[0].labelval;
                $scope.panel_name =  $scope.panelList[0].labelname;
                //料站
                $scope.select_slot_list = [];
                $scope.select_slot_no = '';
                $scope.barcode = '';
                $scope.disable_control = config.cache.sys_parameter["SMTInfo.SMTPartDisableControl"] //禁用物料卡控
            
                // 自動隱蔽鍵盤
                element_barcode_no = document.getElementById('input_barcode');
                makeBarcodeInput(element_barcode_no);
                element_barcode_no.focus();
            }

            $scope.back = function(){
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('menu', true);
            };

            // QRCode輸入
            $scope.QRCodeScanner = function(){
                $scope.$root.OpenScanner({
                    after : function(result){
                        $scope.barcode = result.text;
                        input_barcode_no_callback(result.text);
                    },
                    exception: function(){
                        //目前裝置無法使用相機
                        $scope.$root.showAlert($filter('translate')('common.msg.cant_use_camera'));
                    },
                    orientation : 'portrait'
                });
            };

            // barcode輸入
            $scope.input_barcode = function(barcode){
                input_barcode_no_callback(barcode);
            };

            // 流程狀態
            $scope.PhaseStatus = function(index){
                if($scope.phases_index > index)
                    return ['prev'];
                else if($scope.phases_index == index)
                    return ['now'];
                else
                    return ['next'];
            }

            // 回上一步流程
            $scope.BackToPhase = function (index){
                if($scope.phases_index > index){
                    $scope.phases_index = index;
                    if($scope.level_index == 0 && $scope.phases_index == 0){
                        //回 台車流程
                        $scope.mfu_type_no = '';
                        $scope.mfu_no = '';
                        $scope.mfu_state = '';
                        $scope.machine_no = '';
                        $scope.plot_no = '';
                        $scope.plot_product_no = '';
                        $scope.plot_item_spec = '';
                        $scope.feederlist_no = '';
                        $scope.feederlist_ver = '';
                        $scope.feederlist = [];
                        // 台車是已上料狀態，只能回到台車流程
                        $scope.mfu_state_40 = false;
                    } else if($scope.level_index == 0 && $scope.phases_index == 1){
                        //回 設備流程
                        // 台車是已上料狀態，不能修改資料
                        if($scope.mfu_state_40 != true){
                            $scope.machine_no = '';
                            $scope.plot_no = '';
                            $scope.plot_product_no = '';
                            $scope.plot_item_spec = '';
                            $scope.feederlist_no = '';
                            $scope.feederlist_ver = '';
                            $scope.feederlist = [];
                        } else {
                            $scope.phases_index = 3;
                            $scope.$root.showAlert($filter('translate')('SMT02.msg.mfu_already_feeding_canNot_modify_data'));
                        };
                        
                    }else if($scope.level_index == 0 && $scope.phases_index == 2){
                        //回 生產批流程
                        // 台車是已上料狀態，不能修改資料
                        if($scope.mfu_state_40 != true){
                            $scope.plot_no = '';
                            $scope.plot_product_no = '';
                            $scope.plot_item_spec = '';
                            $scope.feederlist_no = '';
                            $scope.feederlist_ver = '';
                            $scope.feederlist = [];
                        } else {
                            $scope.phases_index = 3;
                            $scope.$root.showAlert($filter('translate')('SMT02.msg.mfu_already_feeding_canNot_modify_data'));
                        };
                    } else if($scope.level_index == 1 && $scope.phases_index == 0){
                        // 回 feeder流程
                        $scope.feeder_no = '';
                        $scope.feeder_state = '';
                        $scope.reel_no = '';
                        $scope.material_no = '';
                        $scope.material_item_spec = '';
                        $scope.slot_no = '';
                        $scope.select_slot_no = '';
                        $scope.select_slot_list = [];
                    } else if($scope.level_index == 1 && $scope.phases_index == 1){
                        //回 物料流程
                        $scope.reel_no = '';
                        $scope.material_no = '';
                        $scope.material_item_spec = '';
                        $scope.slot_no = '';
                        $scope.select_slot_no = '';
                        $scope.select_slot_list = [];
                    }
                }
            }

            // 切換頁面 ng-class
            $scope.LevelPhaseStatus = function(index){
                return $scope.level_index == 0?'prevPage':'nextPage';
            }

            // 下一頁
            $scope.nextPage = function(){
                if($scope.level_index == 0 && $scope.phases_index == 3){
                    if($scope.feederlist_no && $scope.feederlist_no != ''){
                        $scope.level_index = 1;
                        $scope.phases_index = 0;
                        $scope.phases = levels[$scope.level_index];
                        $timeout(function(){
                            element_barcode_no.focus();
                        })
                    } else {
                        //請先刷入料站表編號
                        $scope.$root.showAlert($filter('translate')('SMT02.msg.feederlist_no_empty'));
                    }
                }
            }

            // 上一頁
            $scope.prevPage = function(){
                if($scope.level_index == 1 && $scope.phases_index == 0){
                    $scope.level_index = 0;
                    $scope.phases_index = 3;
                    $scope.phases = levels[$scope.level_index];
                    $timeout(function(){
                        element_barcode_no.focus();
                    })
                } else {
                    $scope.$root.showConfirm($filter('translate')('SMT02.msg.prev-page_data-disappear'),
                        function(dialog){
                            dialog.hide();
                            $scope.level_index = 0;
                            $scope.phases_index = 3;
                            $scope.phases = levels[$scope.level_index];
                            // 清空 feeder
                            $scope.feeder_no = '';
                            $scope.feeder_state = '';
                            // 清空 物料批
                            $scope.reel_no = '';
                            $scope.material_no = '';
                            $scope.material_item_spec = '';
                            // 清空 料站
                            $scope.slot_no = '';
                            $scope.select_slot_list = [];
                            $scope.select_slot_no = '';
                        }
                    );
                }
            }

            // 上線
            $scope.online = function(){
                var content = {
                    mfu_type_no:  $scope.mfu_type_no,
                    mfu_no :  $scope.mfu_no,
                    mfu_state :  $scope.mfu_state,
                    equipment_no :  $scope.machine_no,
                    plot_no :  $scope.plot_no,
                    feederlist_no :  $scope.feederlist_no,
                    feederlist_ver :  $scope.feederlist_ver
                }
                mfu_equipment_set_process(content, function(){
                    // 已上料台車上線成功後，切回原本的設定
                    $scope.mfu_state_40 = false;
                    $scope.BackToPhase(0);
                });
            }

            // 開窗更換料站表
            $scope.loadFeederList = function(){
                // 台車為已上料狀態，不可更改料站表
                if($scope.mfu_state_40 != true ){
                    $scope.$root.showSelect({
                        label : 'feederlist_ver',
                        code : 'feederlist_ver',
                        selectCode : $scope.feederlist_ver,
                        list : $scope.feederlist,
                        confirm : function(item, dialog){
                            $scope.feederlist_ver = item.feederlist_ver;
                            dialog.hide();
                        }
                    });
                } else {
                    $scope.$root.showAlert($filter('translate')('SMT02.msg.mfu_already_feeding_canNot_modify_feederlist_no'));
                }; 
            };
            //開窗更換正背版
            $scope.loadPanel = function()
            {
                $scope.$root.showSelect({
                    label : 'labelname',
                    code : 'labelval',
                    selectCode : $scope.panel_val,
                    list : $scope.panelList,
                    confirm : function(item, dialog){
                        $scope.panel_val = item.labelval;
                        $scope.panel_name = item.labelname;
                        dialog.hide();
                    }
                });
            };
            $scope.loadSlot = function()
            {
                if($scope.level_index == 1 && $scope.phases_index == 2 && $scope.select_slot_list.length > 0)
                {
                    $scope.$root.showSelect(
                        {
                            label : 'slot_no',
                            code : 'slot_no',
                            selectCode : $scope.select_slot_no,
                            list : $scope.select_slot_list,
                            confirm : function(item, dialog)
                            {
                                $scope.select_slot_no = item.slot_no;
                                // $scope.$root.showConfirm($filter('translate')('SMT.common.msg.sure_submit'),
                                // function(confirm_alter)
                                // {
                                //     var content = {
                                //         mfu_type_no :  $scope.mfu_type_no,
                                //         mfu_no :  $scope.mfu_no,
                                //         mfu_state :  $scope.mfu_state,
                                //         area_no :  $scope.area_no,
                                //         equipment_no :  $scope.machine_no,
                                //         plot_no :  $scope.plot_no,
                                //         feederlist_no :  $scope.feederlist_no,
                                //         feederlist_ver :  $scope.feederlist_ver,
                                //         feeder_type_no :  $scope.feeder_type_no,
                                //         feeder_no :  $scope.feeder_no,
                                //         feeder_state :  $scope.feeder_state,
                                //         material_no :  $scope.material_no,
                                //         reel_no: $scope.reel_no,
                                //         slot_no: $scope.select_slot_no,
                                //         panel_side:$scope.panel_val
                                //     };
                                //     mfu_reel_set_process(content,function(){
                                //         $scope.phases_index = 0;
                                //         $scope.feeder_no = '';
                                //         $scope.feeder_state = '';
                                //         $scope.material_no = '';
                                //         $scope.reel_no = '';
                                //         $scope.material_item_spec = '';
                                //         $scope.slot_no = '';
                                //         $scope.select_slot_no = '';
                                //         $scope.select_slot_list = [];
                                //         $scope.$root.showToast($filter('translate')('common.msg.success'));
                                //     });
                                //     confirm_alter.hide();
                                // });
                                dialog.hide();
                            }
                        });
                }
                else
                {
                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_slot'));
                }
            }
            //取得料站資料(條列式)
            function get_slot_list()
            {
                // var slot_query_condition_info =
                // [
                //     {column_name:'a.EquipmentNo', query_mode:'0', value: $scope.machine_no, merge_condition_model:'AND'},
                //     {column_name:'c.MaterialNo', query_mode:'0', value: $scope.material_no, merge_condition_model:'AND'},
                //     {column_name:'c.FeederListNO', query_mode:'0', value: $scope.feederlist_no, merge_condition_model:'AND'},
                //     {column_name:'c.FeederListVer', query_mode:'0', value: $scope.feederlist_ver, merge_condition_model:'AND'}
                // ];
                let slot_content = {
                    equipment_no:$scope.machine_no,
                    material_no:$scope.material_no,
                    feederlist_no:$scope.feederlist_no,
                    feederlist_ver:$scope.feederlist_ver,
                    panel_side: $scope.panel_val //  (1：正板，2：背板) 
                }
                slot_info_get(slot_content, function(slot_itemList)
                {
                    if(slot_itemList && slot_itemList.length > 0)
                    {
                        $scope.select_slot_list = slot_itemList;
                        $scope.select_slot_no = slot_itemList[0].slot_no;
                    }
                });
            }
            
            function reel_process(itemList){
                // 在庫 0
                if(itemList[0].state == '0'){
                    $scope.phases_index = 2;
                    $scope.reel_no = itemList[0].reel_no;
                    $scope.material_no = itemList[0].material_no;
                    $scope.material_item_spec = itemList[0].item_spec;
                    get_slot_list();
                // 80 報廢
                } else if(itemList[0].state == '80'){
                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_scrapped'));
                // 40 已上料
                } else if (itemList[0].state == '40'){
                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_feeding'));
                // 60 已上線
                } else if (itemList[0].state == '60'){
                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.reel_already_online'));
                // 不存在
                } else {
                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_reel'));
                };
            }

            // barcode輸入 callback
            function input_barcode_no_callback(barcode){
                if(barcode != ''){
                    // 台車流程
                    if($scope.level_index == 0 && $scope.phases_index == 0){
                        // 在庫 or 已上料 狀態才能成功，其他狀態提示異常
                        mfu_info_get(barcode, function(itemList){
                            if(itemList && itemList.length > 0){
                                // 在庫 0
                                if(itemList[0].state == '0'){
                                    // 只有在庫狀態需要檢查台車，已上料狀態不需要檢查
                                    mfu_check_process({
                                        mfu_no: itemList[0].mfu_no
                                    },function(data, response){
                                        if(response.Message != ''){
                                            $scope.$root.showAlert(response.Message);
                                        } else {
                                            $scope.phases_index = 1;
                                            $scope.mfu_type_no = itemList[0].mfu_type_no;
                                            $scope.mfu_no = itemList[0].mfu_no;
                                            $scope.mfu_state = itemList[0].state;
                                        };
                                    });
                                // 已上料 40
                                } else if (itemList[0].state == '40'){
                                    // 台車是已上料的狀態，則直接帶入之前user操作系統所記錄的生產批及料站表資料，且不可修改
                                    $scope.mfu_type_no = itemList[0].mfu_type_no;
                                    equipment_info_get(itemList[0].equipment_no,function(machine_item)
                                    {
                                        $scope.area_no = machine_item[0].area_no;
                                        plot_info_get_dispatch_zero(itemList[0].plot_no,function(poduct_item)
                                        {
                                            $scope.mfu_state_40 = true;
                                            $scope.phases_index = 3;
                                            $scope.mfu_type_no = itemList[0].mfu_type_no;
                                            $scope.mfu_no = itemList[0].mfu_no;
                                            $scope.mfu_state = itemList[0].state;
                                            $scope.machine_no = itemList[0].equipment_no;                               
                                            $scope.plot_no = itemList[0].plot_no;
                                            if(poduct_item.length && poduct_item.length > 0){
                                                $scope.plot_product_no = poduct_item[0].product_no;
                                                $scope.plot_item_spec = poduct_item[0].item_spec;
                                            }
                                            $scope.feederlist_no = itemList[0].feederlist_no;
                                            $scope.feederlist_ver = itemList[0].feederlist_ver;

                                        });
                                    });
                                // 報廢 80
                                } else if (itemList[0].state == '80'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.mfu_scrapped'));
                                // 送修 70
                                } else if (itemList[0].state == '70'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.mfu_repair'));
                                // 已上線 60
                                } else if (itemList[0].state == '60'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.mfu_already_online'));
                                // 不存在
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_mfu'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        })
                    // 設備流程
                    } else if($scope.level_index == 0 && $scope.phases_index == 1) {
                        equipment_info_get(barcode, function(itemList){
                            if(itemList && itemList.length > 0){
                                $scope.phases_index = 2;
                                $scope.machine_no = itemList[0].equipment_no;
                                $scope.area_no = itemList[0].area_no
                                $scope.is_dispatch = itemList[0].is_dispatch;
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 生產批流程
                    } else if($scope.level_index == 0 && $scope.phases_index == 2) {
                        if($scope.is_dispatch == 0){
                            plot_info_get_dispatch_zero(barcode, function(itemList){
                                if(itemList && itemList.length > 0){
                                    $scope.phases_index = 3;
                                    $scope.plot_no = itemList[0].plot_no;
                                    $scope.plot_product_no = itemList[0].product_no;
                                    $scope.plot_item_spec = itemList[0].item_spec;
                                    $scope.feederlist_no = itemList[0].feederlist_no;
                                    $scope.feederlist_ver = itemList[0].feederlist_ver;
                                    $scope.feederlist = itemList;
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        } else if ($scope.is_dispatch == 1){
                            plot_info_get_dispatch_one(barcode, function(itemList){
                                if(itemList && itemList.length > 0){
                                    $scope.phases_index = 3;
                                    $scope.plot_no = itemList[0].plot_no;
                                    $scope.plot_product_no = itemList[0].product_no;
                                    $scope.plot_item_spec = itemList[0].item_spec;
                                    $scope.feederlist_no = itemList[0].feederlist_no;
                                    $scope.feederlist_ver = itemList[0].feederlist_ver;
                                    $scope.feederlist = itemList;
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        } else {
                            $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                        };
                    // 料站表流程
                    } else if($scope.level_index == 0 && $scope.phases_index == 3) {
                        if($scope.mfu_state_40 != true){
                            feederlist_info_get(barcode, function(itemList){
                                if(itemList && itemList.length > 0){
                                    $scope.feederlist = itemList;
                                    $scope.feederlist_no = itemList[0].feederlist_no;
                                    $scope.feederlist_ver = itemList[0].feederlist_ver;
                                    $scope.check_forbidden_reel = itemList[0].check_forbidden_reel;//禁用物料參數
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        } else {
                            $scope.$root.showAlert($filter('translate')('SMT02.msg.mfu_already_feeding_canNot_modify_feederlist_no'));
                        };
                    // feeder流程
                    } else if($scope.level_index == 1 && $scope.phases_index == 0) {
                        feeder_info_get(barcode, function(itemList){
                            // 在台車上料功能，在庫狀態和已上料狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length > 0){
                                // 0 在庫
                                if(itemList[0].state == '0'){
                                    // 在庫狀態才做檢查
                                    feeder_check_process({
                                        feeder_no : itemList[0].feeder_no
                                    },function(data, response){
                                        if(response.Message != ''){
                                            $scope.$root.showAlert(response.Message);
                                        } else {
                                            $scope.phases_index = 1;
                                            $scope.feeder_type_no = itemList[0].feeder_type_no;
                                            $scope.feeder_no = itemList[0].feeder_no;
                                            $scope.feeder_state = itemList[0].state;
                                        };
                                    });
                                // 40 已上料 → 如果 feeder 是已上料狀態，會同時刷入 feeder 和 捲料(物料)資料
                                } else if(itemList[0].state == '40'){
                                    $scope.phases_index = 2;
                                    $scope.feeder_type_no = itemList[0].feeder_type_no;
                                    $scope.feeder_no = itemList[0].feeder_no;
                                    $scope.feeder_state = itemList[0].state;
                                    $scope.material_no = itemList[0].material_no;
                                    $scope.reel_no = itemList[0].reel_no;
                                    $scope.material_item_spec = itemList[0].item_spec;
                                    get_slot_list();
                                 // 80 報廢
                                } else if(itemList[0].state == '80'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_scrapped'));
                                // 70 送修
                                } else if(itemList[0].state == '70'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_repair'));
                                // 50 已上台車
                                } else if (itemList[0].state == '50'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_already_feeding'));
                                // 60 已上線
                                } else if (itemList[0].state == '60'){
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.feeder_already_online'));
                                // 不存在
                                } else {
                                    $scope.$root.showAlert($filter('translate')('SMT.common.msg.no_feeder'));
                                };
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 物料流程
                    } else if($scope.level_index == 1 && $scope.phases_index == 1) {
                        material_info_get(barcode, function(itemList){
                            // 在庫狀態才能成功，其他狀態提示異常
                            if(itemList && itemList.length > 0){
                                if($scope.disable_control == '1'){ //啟用禁用物料流程
                                    if(itemList[0].forbidden_reel == '1'){
                                        $scope.$root.showAlert($filter('translate')('fordden_msg.forbid_reel'));
                                    }else{
                                        // MSD卡控卡這裡
                                        if((+itemList[0].remain_time) < 0 && itemList[0].remain_time !='-0.1'){
                                            $('#toast-panel').css("background-color","rgb(219, 75, 60)");
                                            $scope.$root.showAlert($filter('translate')('SMT03.msg.MSD_over_time'));
                                            return;
                                        }else if((+itemList[0].remain_time) <= 30 && (+itemList[0].remain_time) > 0){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_in_a_hurry'));
                                            return
                                        }else if((+itemList[0].remain_time) <= 60 && (+itemList[0].remain_time) > 30){
                                            reel_process(itemList);
                                            $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_go_to_over_time'));
                                            return
                                        }else{
                                            reel_process(itemList);
                                        }
                                    }
                                }else{
                                    // MSD卡控卡這裡
                                    if((+itemList[0].remain_time) < 0 && itemList[0].remain_time !='-0.1'){
                                        $('#toast-panel').css("background-color","rgb(219, 75, 60)");
                                        $scope.$root.showAlert($filter('translate')('SMT03.msg.MSD_over_time'));
                                        return;
                                    }else if((+itemList[0].remain_time) <= 30 && (+itemList[0].remain_time) > 0){
                                        reel_process(itemList);
                                        $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_in_a_hurry'));
                                        return
                                    }else if((+itemList[0].remain_time) <= 60 && (+itemList[0].remain_time) > 30){
                                        reel_process(itemList);
                                        $rootScope.showAlertWithoutfocus($filter('translate')('SMT03.msg.MSD_go_to_over_time'));
                                        return
                                    }else{
                                        reel_process(itemList);
                                    }
                                }
                            } else {
                                $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                            };
                        });
                    // 料站流程
                    } else if($scope.level_index == 1 && $scope.phases_index == 2) {
                        // var slot_query_condition_info =
                        // [
                        //     {column_name:'a.EquipmentNo', query_mode:'0', value: $scope.machine_no, merge_condition_model:'AND'},
                        //     {column_name:'a.SlotNo', query_mode:'0', value: barcode, merge_condition_model:'AND'},
                        //     {column_name:'c.FeederListNO', query_mode:'0', value: $scope.feederlist_no, merge_condition_model:'AND'},//20200604 新增兩筆資料到後端 modify by Oliver for M#72980
                        //     {column_name:'c.FeederListVer', query_mode:'0', value: $scope.feederlist_ver, merge_condition_model:'AND'}//20200604 新增兩筆資料到後端 modify by Oliver for M#72980
                        // ];
                        let slot_content = {
                            equipment_no:$scope.machine_no,
                            material_no:$scope.material_no,
                            feederlist_no:$scope.feederlist_no,
                            feederlist_ver:$scope.feederlist_ver,
                            panel_side: $scope.panel_val //  (1：正板，2：背板) 
                        }

                        if(getCheckResult(barcode)){//如果檢核結果為真
                            slot_info_get(slot_content, function(itemList){
                                if(itemList && itemList.length > 0){
                                    $scope.slot_no = barcode;
                                    $scope.select_slot_no = itemList[0].slot_no;
                                    var content = {
                                        mfu_type_no :  $scope.mfu_type_no,
                                        mfu_no :  $scope.mfu_no,
                                        mfu_state :  $scope.mfu_state,
                                        area_no :  $scope.area_no,
                                        equipment_no :  $scope.machine_no,
                                        plot_no :  $scope.plot_no,
                                        feederlist_no :  $scope.feederlist_no,
                                        feederlist_ver :  $scope.feederlist_ver,
                                        feeder_type_no :  $scope.feeder_type_no,
                                        feeder_no :  $scope.feeder_no,
                                        feeder_state :  $scope.feeder_state,
                                        material_no :  $scope.material_no,
                                        reel_no: $scope.reel_no,
                                        slot_no: $scope.slot_no,
                                        panel_side:$scope.panel_val
                                    };
                                    mfu_reel_set_process(content,function(){
                                        $scope.phases_index = 0;
                                        $scope.feeder_no = '';
                                        $scope.feeder_state = '';
                                        $scope.material_no = '';
                                        $scope.reel_no = '';
                                        $scope.material_item_spec = '';
                                        $scope.slot_no = '';
                                        $scope.select_slot_no = '';
                                        $scope.select_slot_list = [];
                                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                                    });
                                } else {
                                    $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                                };
                            });
                        }else{
                            $scope.$root.showAlert($filter('translate')('SMT02.msg.slot_no_not_right'));
                        }
                    } else {
                        $scope.$root.showAlert($filter('translate')('SMT.common.msg.process_error'));
                    };
                }
            };

             // 料站資訊取得
             function getCheckResult(barcode){
                if($scope.select_slot_list.length > 0){//檢查可用那些料站口
                    return $scope.select_slot_list.some(function(item){return item.slot_no == barcode;})
                }else{
                    return true;
                }
            }

            $scope.check = function(){
                var content = {
                    mfu_no: $scope.mfu_no,
                    feederlist_no: $scope.feederlist_no,
                    feederlist_ver: $scope.feederlist_ver,
                    feeder_no: $scope.feeder_no,
                    equipment_no:$scope.machine_no,
                    panel_side:$scope.panel_val
                };
                // 取得上料紀錄
                feeding_record_info_get(content, function(data){
                    if(data && data.mfu_check_list_data.length > 0){
                        $scope.feeding_record_list = data.mfu_check_list_data;
                    } else {
                        $scope.feeding_record_list = [];
                    };
                    
                    $MaterialStation.init({
                        data:{
                            plot_no: $scope.plot_no,
                            product_no : $scope.plot_product_no,
                            item_spec : $scope.plot_item_spec,
                        },
                        itmeList: $scope.feeding_record_list || []    
                    });
                });
            };

            // 取得 台車資料
            function mfu_info_get(mfu_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'mfu_info_get',
                    query_condition_info:[
                        {column_name:'MFUNo', query_mode:'0', value: mfu_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 台車檢查
            function mfu_check_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_mfu_check_process',
                    content : content,
                    success : function(data, response){
                        if(callback)
                            callback(data, response);
                    }
                });
            };

            // 取得 設備資料
            function equipment_info_get(machine_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'mfu_equipment_info_get',
                    query_condition_info:[
                        {column_name:'a.MFUTypeNo', query_mode:'0', value: $scope.mfu_type_no, merge_condition_model:'AND'},
                        {column_name:'a.EquipmentNo', query_mode:'0', value: machine_no, merge_condition_model:'AND'}
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 取得 生產批資料 (is_dispatch = 0)
            function plot_info_get_dispatch_zero(plot_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'smt_equipment_queue_info_get',
                    query_condition_info:[
                        {column_name:'a.AreaNo', query_mode:'0', value: $scope.area_no, merge_condition_model:'AND'},
                        {column_name:'b.BaseLotNo', query_mode:'0', value: plot_no, merge_condition_model:'AND'}
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 取得 生產批資料 (is_dispatch = 1)
            function plot_info_get_dispatch_one(plot_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'equipment_dispatch_queue_info_get',
                    query_condition_info:[
                        {column_name:'a.EquipmentNo', query_mode:'0', value: $scope.area_no, merge_condition_model:'AND'},
                        {column_name:'b.BaseLotNo', query_mode:'0', value: plot_no, merge_condition_model:'AND'}
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // 取得 料站表資料
            function feederlist_info_get(feederlist_no, callback){
                let content = {
                    FeederListNo:feederlist_no,
                    BaseLotNo:$scope.plot_no,
                    EquipmentNo:$scope.machine_no
                }
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feederlist_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 取得 Feeder資料
            function feeder_info_get(feeder_no, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : 'feeder_info_get',
                    query_condition_info:[
                        {column_name:'FeederNo', query_mode:'0', value: feeder_no, merge_condition_model:'AND'},
                    ]
                }, function(itemList){
                    if(callback)
                        callback(itemList);
                });
            };

            // Feeder檢查
            function feeder_check_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.feeder_mfu_check_process',
                    content : content,
                    success : function(data, response){
                        if(callback)
                            callback(data, response);
                    }
                });
            };

            // 取得 捲料資料
            function material_info_get(reel_no, callback){
                let content = {
                    reel_no:reel_no,
                    feederlist_no:$scope.feederlist_no,
                    feederlist:$scope.feederlist,
                    feederlist_ver:$scope.feederlist_ver,
                    check_forbidden_reel:$scope.check_forbidden_reel
                }
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.reel_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            
            function slot_info_get(content,callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.uncomplete_slot_info_get',
                    content : content,
                    success : function(data){
                        if(data.slot_list.length == 0){
                            $scope.$root.showAlert($filter('translate')('SMT02.msg.slot_list_none'));                    
                        }else{
                            if(callback)
                                callback(data.slot_list);
                        }
                    }
                });
            };

            // 比對料站表並回寫資料
            function mfu_reel_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.mfu_reel_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 台車上線
            function mfu_equipment_set_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.mfu_equipment_set_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 點擊檢查 取得上料紀錄
            function feeding_record_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSMT.Module_SMT.mfu_check_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            $scope.input_PDA_barcode = function(event){
                if (event.keyCode == 13) {
                    element_barcode_no.blur();
                    input_barcode_no_callback($scope.barcode)
                    $scope.barcode = '';
                } else {
                    $scope.barcode = $scope.barcode || '';
                }
            }

            //20200914 modify by Dustdusk for M#: 增加
            function makeBarcodeInput(el) {
                var _input = el.getElementsByClassName('barcode_input')[0];
                //var _mask = el.getElementsByClassName('barcode_mask')[0];
                el.focus = function () {
                    //e.preventDefault();
                    //_mask.click();
                    el.click();
                }
                el.addEventListener('click', function (e) {
                    e.preventDefault();
                    //_input.value = '';
                    if (document.activeElement !== _input)
                        _input.focus();
                });
                _input.addEventListener('focus', function (e) {
                    e.preventDefault();
                    setTimeout(function () {
                        _input.removeAttribute('readonly');
                    }, 200);
                });
                _input.addEventListener('blur', function (e) {
                    e.preventDefault();
                    _input.setAttribute('readonly', 'readonly');
                });
            }
        }
    ];
});
